package com.huidu.bitrade.service;

import com.huidu.bitrade.constant.BooleanEnum;
import com.huidu.bitrade.constant.ProhibitType;
import com.huidu.bitrade.dao.ExchangeProhibitConfigDao;
import com.huidu.bitrade.entity.ExchangeProhibitConfig;
import com.huidu.bitrade.entity.Member;
import com.huidu.bitrade.entity.QExchangeProhibitConfig;
import com.huidu.bitrade.exception.ServiceException;
import com.huidu.bitrade.service.Base.BaseService;
import com.huidu.bitrade.util.PredicateUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author QQ：97095646
 * @description
 * @date 2019/1/11 13:45
 */
@Service
public class ExchangeProhibitConfigService extends BaseService {
    @Autowired
    private ExchangeProhibitConfigDao baseDao;

    @Autowired
    private MemberService memberService;

    public boolean memberForbidWithdraw(long memberId) {
        return memberForbid(memberId, ProhibitType.WITHDRAW);
    }

    public boolean memberForbidRecharge(long memberId) {
        return memberForbid(memberId, ProhibitType.RECHARGE);
    }

    public boolean memberForbidOtc(long memberId) {
        return memberForbid(memberId, ProhibitType.OTC);
    }

    public boolean memberForbid(long memberId, ProhibitType type) {
        List<BooleanExpression> booleanExpressions = new ArrayList<>();
        booleanExpressions.add(QExchangeProhibitConfig.exchangeProhibitConfig.memberId.eq(memberId));
        booleanExpressions.add(QExchangeProhibitConfig.exchangeProhibitConfig.type.eq(type));
        Predicate predicate = PredicateUtils.getPredicate(booleanExpressions);
        return Objects.isNull(predicate) || baseDao.exists(predicate);
    }

    public void checkOtcForbid(Long memberId, Long otcMemberId) throws ServiceException {
        if (memberId != null && memberForbidOtc(memberId)) {
            throw new ServiceException("您暂时被禁止otc交易");
        }

        if (otcMemberId != null && memberForbidOtc(otcMemberId)) {
            throw new ServiceException("此商家暂时被禁止otc交易");
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void prohibitMember(Long memberId, BooleanEnum status, ProhibitType prohibitType) {
        Member member = memberService.findOne(memberId);
        Assert.notNull(member, "用户不存在");
        if (BooleanEnum.IS_FALSE == status) {
            if (!memberForbid(memberId, prohibitType)) {
                ExchangeProhibitConfig config = new ExchangeProhibitConfig();
                config.setMemberId(memberId);
                config.setType(prohibitType);
                baseDao.save(config);
            }
        } else if (BooleanEnum.IS_TRUE == status) {
            //解禁
            releaseProhibit(memberId, prohibitType);
        }
    }
    public void releaseProhibit(Long memberId, ProhibitType prohibitType) {
        baseDao.deleteByMemberIdAndType(memberId,prohibitType);
    }


}
